package com.bygf.curve.demo;

import com.bygf.curve.Curve;
import com.bygf.curve.GradientPara;
import com.bygf.curve.LinearGradientCurve;
import com.bygf.curve.panel.ChartPanel;
import com.bygf.curve.utils.ToolTipFormat;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JColorChooser;

/**
 *
 * @author sunyupei
 */
public class LinearGradientCurveDemo1 extends javax.swing.JPanel {

    ChartPanel chartPanel = new ChartPanel();
    List<GradientPara> paras = new ArrayList<>();
    LinearGradientCurve sin = new LinearGradientCurve("正弦曲线", 0.5F);

    /**
     * Creates new form XYCurve
     */
    public LinearGradientCurveDemo1() {
        initComponents();
        paraPanel.setSize(paraPanel.getWidth(), 57);
        chartPanel = new ChartPanel("渐变区域曲线演示", "x", "y", "", false);
        chartPanel.setBounds(0, 0, this.getWidth(), this.getHeight() - paraPanel.getHeight() - 5);
        chartPanel.getYAxis().setStartValue(0);
        chartPanel.getYAxis().setEndValue(100);
        chartPanel.getYAxis().setDecimalFormat(new DecimalFormat("#.##"));
        chartPanel.getXAxis().setStartValue(0);
        chartPanel.getXAxis().setEndValue(360);
        chartPanel.getTitlePanel().setTitleFont(new Font("宋体", Font.BOLD, 26));
        List<Curve> curveList = new ArrayList<>();

        sin.setToolTipFormat(new ToolTipFormat("{0}:sin({1})={2}", null, new DecimalFormat("#.##")));
        for (int i = 0; i < 360; i++) {
            sin.addItem(i, Math.sin(Math.toRadians(i)) * 50 + 50);
        }
        sin.getRender().setShapeVisible(true);
        curveList.add(sin);
        GradientPara p1 = new GradientPara(00, Color.GREEN);
        GradientPara p2 = new GradientPara(50, Color.YELLOW);
        GradientPara p3 = new GradientPara(100, Color.RED);
        paras.add(p1);
        paras.add(p2);
        paras.add(p3);
        sin.setGradientParas(paras);
        chartPanel.getCurvePanel().setCurveList(curveList);
        chartPanel.getCurvePanel().setBackColor(Color.WHITE);
        this.add(chartPanel);
        this.updateUI();
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        this.updateUI();
        chartPanel.setBounds(0, 0, this.getWidth(), this.getHeight() - paraPanel.getHeight() - 5);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        paraPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        paraSpinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        valueText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        ColorLabel = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        alphaText = new javax.swing.JTextField();

        paraPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("渐变参数"));

        jLabel1.setText("参数：");

        paraSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 3, 1));
        paraSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                paraSpinnerStateChanged(evt);
            }
        });

        jLabel2.setText("数值：");

        valueText.setText("0");
        valueText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                valueTextKeyReleased(evt);
            }
        });

        jLabel3.setText("颜色：");

        ColorLabel.setBackground(new java.awt.Color(0, 255, 0));
        ColorLabel.setEnabled(false);
        ColorLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                ColorLabelMouseReleased(evt);
            }
        });

        jLabel4.setText("透明度：");

        alphaText.setText("0.5");
        alphaText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                alphaTextKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout paraPanelLayout = new javax.swing.GroupLayout(paraPanel);
        paraPanel.setLayout(paraPanelLayout);
        paraPanelLayout.setHorizontalGroup(
            paraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paraPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(paraSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(valueText, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ColorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(alphaText, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(140, Short.MAX_VALUE))
        );
        paraPanelLayout.setVerticalGroup(
            paraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paraPanelLayout.createSequentialGroup()
                .addGroup(paraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(paraSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(valueText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(ColorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(alphaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 10, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(paraPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 350, Short.MAX_VALUE)
                .addComponent(paraPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void paraSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_paraSpinnerStateChanged
        GradientPara para = paras.get(Integer.parseInt(paraSpinner.getValue().toString()) - 1);
        DecimalFormat df = new DecimalFormat("#.#");
        valueText.setText(df.format(para.getValue()));
        ColorLabel.setBackground(para.getColor());
    }//GEN-LAST:event_paraSpinnerStateChanged

    private void ColorLabelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ColorLabelMouseReleased
        Color c = JColorChooser.showDialog(this, "颜色选择", ColorLabel.getBackground());
        if (c != null) {
            ColorLabel.setBackground(c);
            GradientPara para = paras.get(Integer.parseInt(paraSpinner.getValue().toString()) - 1);
            para.setColor(c);
            chartPanel.getCurvePanel().repaint();
        }
    }//GEN-LAST:event_ColorLabelMouseReleased

    private void valueTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_valueTextKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                double value = Double.parseDouble(valueText.getText());
                int index = Integer.parseInt(paraSpinner.getValue().toString()) - 1;
                GradientPara para = paras.get(Integer.parseInt(paraSpinner.getValue().toString()) - 1);
                if (index == 0 && value >= paras.get(1).getValue()) {
                    return;
                } else if (index == 2 && value <= paras.get(1).getValue()) {
                    return;
                } else if (index == 1 && (value <= paras.get(0).getValue() || value >= paras.get(2).getValue())) {
                    return;
                }
                para.setValue(value);
                chartPanel.getCurvePanel().repaint();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_valueTextKeyReleased

    private void alphaTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_alphaTextKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                float alhpa = Float.parseFloat(alphaText.getText());
                sin.setAlpha(alhpa);
                chartPanel.getCurvePanel().repaint();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_alphaTextKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ColorLabel;
    private javax.swing.JTextField alphaText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel paraPanel;
    private javax.swing.JSpinner paraSpinner;
    private javax.swing.JTextField valueText;
    // End of variables declaration//GEN-END:variables
}
